package com.jdew.thkj.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.FreeBean;
import com.jdew.thkj.bean.ZeroBuyBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class FreeZRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    public  List<FreeBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemClickListener mItemClickListener;

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public FreeZRAdapter(Context context, List<FreeBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<FreeBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate(R.layout.item_zero, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FreeAdapter.MyViewHolder myviewHolder = (FreeAdapter.MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView beforePrice;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.shopname)
        TextView shopname;


        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            name.setText(dataList.get(position).getTitle());
            num.setText("已售" + dataList.get(position).getSales());
            shopname.setText(dataList.get(position).getShopName());
            beforePrice.setText("原价: ¥" + dataList.get(position).getOriginalPrice());
            beforePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            RequestOptions options = new RequestOptions().override(400, 400).placeholder(R.mipmap.ploder).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
            Glide.with(context).load(dataList.get(position)).apply(options).into(product);
        }
    }
}
